<?php

namespace app\merchants\services;

use app\common\loader\ModelMerchant;
use app\common\loader\Services;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

/**
 * 积分商城管理
 */
class PointService
{
    /**
     * 商品列表
     * @param $options
     * @return array
     * @throws Exception
     * @throws DbException
     */
    public function getPage($options)
    {
        $options['order'] = 'id desc';
        $list = ModelMerchant::MerchantProductPoint()->page($options);
        Services::Helper()->showListTitle($list['data'], ['product_point_class' => 'class_id']);
        return $list;
    }

    /**
     * 商品列表
     * @param $options
     * @return array
     * @throws Exception
     * @throws DbException
     */
    public function getList($options , $limit)
    {
        return ModelMerchant::MerchantProductPoint()->where($options)->limit($limit)->select();
    }


    /**
     * 修改状态
     * @param array $where
     * @param array $update
     * @return bool
     * @throws DbException
     */
    public function updateField(array $where, array $update): bool
    {
        unset($update['id']);
        return ModelMerchant::MerchantProductPoint()->where($where)
            ->update(null, $update);
    }


    /**
     * 添加、修改
     * @param array $data | 更新插入数据
     * @return bool
     * @throws Exception
     * @throws DbException
     */
    public function save(array $data): bool
    {
        return ModelMerchant::MerchantProductPoint()->proSave($data);
    }


    /**
     * 软删除
     * @param int $id | 主键
     * @throws \Exception
     */
    public function del(int $id, $mer_id): void
    {
        $data = ModelMerchant::MerchantProductPoint()->where(['id' => $id, 'mer_id' => $mer_id])
            ->update(null, ['delete_at' => date('Y-m-d H:i:s')]);
        if (!$data) {
            throw new Exception('删除失败', API_ERROR);
        }
    }

    /**
     * 商品详情
     * @param array $where
     * @return array|\think\Model|null
     * @throws Exception
     */
    public function info(array $where)
    {
        $info = ModelMerchant::MerchantProductPoint()->where($where)->find();
        if (empty($info)) {
            api_exception(400, '积分商品不存在或已删除，请稍后再试');
        }
        return $info;
    }

    /**
     * 积分订单
     * @param array $options
     * @param int $type
     * @return array
     * @throws Exception
     * @throws DbException
     */
    public function getOrderPage(array $options, int $type): array
    {
        $options['order'] = 'id desc';
        $list = ModelMerchant::MerchantOrderPoint()->page($options);
        $dictS = [];
        if ($type === USER_TYPE_SALESMAN) {
            $dictS = ['salesman' => 'user_id'];
        } elseif ($type === USER_TYPE_MEMBER) {
            $dictS = ['member' => 'user_id'];
        }
        if (!empty($list['data']) && !empty($dictS)) {
            Services::Helper()->showListTitle($list['data'], $dictS);
        }
        return $list;
    }


    /**
     * 积分订单发货
     * @param $order_no |订单
     * @param $express_company |快递公司
     * @param $express_no |快递单号
     * @param $op_user |操作人
     * @throws Exception
     * @throws DbException
     */
    public function OrderSend($order_no, $express_company, $express_no, $op_user , $mer_id, $user_type)
    {
        $model = ModelMerchant::MerchantOrderPoint();
        $orderInfo =$model->orderInfo(['order_no' => $order_no,'mer_id' => $mer_id,'user_type' => $user_type]);
        if (empty($orderInfo)){
            throw new Exception('订单未找到',API_ERROR);
        }
        if ($orderInfo['status'] !== ORDER_STATUS_WAIT_GOODS){
            throw new Exception('订单非待发货订单',API_ERROR);
        }
        $model->removeOption()->where(['order_no' => $order_no,'mer_id' => $mer_id,'user_type' => $user_type])->update(null,[
            'express_company' => $express_company,
            'express_no' => $express_no,
            'op_user' => $op_user,
            'status' => ORDER_STATUS_SEND_GOODS,
        ]);

    }


    /**
     * 轮播图列表
     * @param int $id | 主键
     * @throws \Exception
     */
    public function banner_list($where)
    {
        return ModelMerchant::MerchantPointMallBanner()->list($where);
    }

    /**
     * 轮播图详情
     * @param int $id | 主键
     * @throws \Exception
     */
    public function banner_info($where)
    {
        $data = ModelMerchant::MerchantPointMallBanner()->info($where);
        if (empty($data)){
            throw  new Exception('信息不存在',API_ERROR);
        }
        return $data;
    }


    /**
     * 轮播图删除
     * @param int $id | 主键
     * @throws \Exception
     */
    public function banner_del($where)
    {
        return ModelMerchant::MerchantPointMallBanner()->del($where);
    }

    /**
     * 轮播图保存
     * @param $data
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function banner_save($data)
    {
        return ModelMerchant::MerchantPointMallBanner()->save($data);
    }
}
